﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.Image
{
    public static class ImageHelper
    {
        public static Bitmap ConvertStringToBitmap(string base64string)
        {
            byte[] bt = Convert.FromBase64String(base64string.Split(',')[1]);
            MemoryStream h_stream = new MemoryStream(bt);
            Bitmap h_bitmap = new Bitmap(h_stream);
            return h_bitmap;
        }
        public static string GetBase64FromImage(Bitmap bmp)
        {
            string strbaser64 = "";
            try
            {
                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                strbaser64 = "data:image/jpeg;base64," + Convert.ToBase64String(arr);
            }
            catch (Exception ex)
            {
               
            }
            return strbaser64;
        }
        public static string GetBase64FromImage1(Bitmap bmp)
        {
            string strbaser64 = "";
            try
            {
                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                strbaser64 = Convert.ToBase64String(arr);
            }
            catch (Exception)
            {

            }
            return strbaser64;
        }
    }
}
